home *** CD-ROM | disk | FTP | other *** search
- Imports System.Threading
-
- ' a class that lets you share data with another thread
-
- Class ThreadData
- Public Id As Integer
- Public Msg As String
- Public Done As Boolean
-
- ' The entry point for the thread
- Sub DoTheTask()
- Dim i As Integer
- For i = 1 To 10
- ' just show that the thread got the correct values
- Console.WriteLine("{0} (Thread ID = {1})", Msg, Id)
- ' Wait for .2 seconds.
- Thread.CurrentThread.Sleep(200)
- Next
- ' Signal that this thread has completed.
- Done = True
- End Sub
- End Class
-
- ' a class that lets you share data with another thread
- ' and that raises an event when the tasks has completed
-
- Class ThreadData2
- Event TaskCompleted(ByVal sender As Object, ByVal endTime As Date)
-
- Public Id As Integer
- Public Msg As String
- Public Done As Boolean
-
- ' The entry point for the thread
- Sub DoTheTask()
- Dim i As Integer
- For i = 1 To 10
- ' just show that the thread got the correct values
- Console.WriteLine("{0} (Thread ID = {1})", Msg, Id)
- ' Wait for .2 seconds.
- Thread.CurrentThread.Sleep(200)
- Next
- ' Signal that this thread has completed.
- Done = True
-
- ' raise an event
- RaiseEvent TaskCompleted(Me, Date.Now)
- End Sub
- End Class
-
- ' this class is a base class for a thread wrapper
-
- MustInherit Class ThreadWrapperBase
- ' This public member exposes the Thread object.
- Public ReadOnly Thread As System.Threading.Thread
-
- ' The constructor creates the thread object and runs the code
- Sub New()
- Me.Thread = New System.Threading.Thread(AddressOf Me.RunThread)
- End Sub
-
- ' This starts the thread
- Overridable Sub Start()
- Me.Thread.Start()
- End Sub
-
- ' This private procedure is where the thread starts its execution;
- ' when the thread terminates, the Done flag is set to
- Private Sub RunThread()
- m_Done = False
- OnStart()
- m_Done = True
- End Sub
-
- Dim m_Done As Boolean
-
- ' This property returns True if the thread has completed its task.
- ReadOnly Property Done() As Boolean
- Get
- Return m_Done
- End Get
- End Property
-
- ' Derived classes must override this to provide the actual code for the thread.
- Protected MustOverride Sub OnStart()
- End Class
-
- ' an example of a thread wrapper
-
- Class MyTask
- Inherits ThreadWrapperBase
-
- ' Instance data
- Public Id As Integer
- Public Msg As String
-
- Sub New(ByVal id As Integer, ByVal msg As String)
- Me.Id = id
- Me.Msg = msg
- End Sub
-
- ' The code that the thread must execute
- Protected Overrides Sub OnStart()
- Dim i As Integer
- For i = 1 To 10
- ' just show that the thread got the correct values.
- Console.WriteLine("{0} (Thread ID = {1})", Msg, Id)
- ' Wait for .2 seconds.
- Thread.CurrentThread.Sleep(200)
- Next
- End Sub
-
- End Class
-
- ' this class is used to demonstrate the ThreadPool
-
- Class LightweightTask
- Public SomeData As String
-
- ' The method that contains the interesting code
- ' (Not really interesting in this example).
- Sub DoTheTask(ByVal state As Object)
- Console.WriteLine("Message from thread #" & state.ToString)
- End Sub
- End Class
-
- ' this class demonstrates the Synchronization attribute
-
- <System.Runtime.Remoting.Contexts.Synchronization()> _
- Class Display
- ' Synchronized classes must inherit from ContextBoundObject.
- Inherits ContextBoundObject
-
- Sub DisplayData()
- Console.Write("Message ")
- Console.Write("from ")
- Console.WriteLine(Thread.CurrentThread.Name)
- End Sub
- End Class
-
- ' this class is used to test the AutoResetEvent class
-
- Class FileFinder
- Dim StartPath As String ' The starting search path
- Dim SearchedPattern As String ' The search pattern (can contain wildcards)
-
- Sub New(ByVal path As String, ByVal search As String)
- Me.StartPath = path
- Me.SearchedPattern = search
- End Sub
-
- ' This is the method with which thread execution starts.
- Sub StartSearch()
- Search(Me.StartPath)
- ' Decrease the number of running threads before exiting.
- Interlocked.Decrement(searchingThreads)
- are.Set()
- End Sub
-
- ' This recursive procedure does the actual job.
- Sub Search(ByVal path As String)
- Dim files() As String
- ' Get all the files that match the search pattern.
- files = System.IO.Directory.GetFiles(path, SearchedPattern)
- ' If there is at least one file, let the main thread know about it.
- If Not files Is Nothing Then
- ' Get a lock on the result ArrayList.
- SyncLock filesAl.SyncRoot
- ' Add all found files.
- filesAl.AddRange(files)
- ' Let the consumer thread know about the new filenames.
- are.Set()
- End SyncLock
- End If
-
- ' Repeat the search on all subdirectories.
- Dim dirname As String
- For Each dirname In System.IO.Directory.GetDirectories(path)
- Search(dirname)
- Next
- End Sub
- End Class
-
-